/*
 *	Copyright 2011 cheol-dong, choi, twitter @aucd29
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sarangnamu.android;

import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


public class ImageListAdapter extends BaseAdapter {

	ImageListAdapter(Context context, int id) {
		super();

		_context = context;
		_listId = id;
		_id = new ArrayList<Integer>();
		_list = new ArrayList<ImageListViewType>();
	}


	/*
	 * public method
	 */
	public void add(ImageListViewType list) {
		_list.add(list);
	}

	/*public boolean isSelectable(int position) {
		try {
			return _list.get(position).isSelectable();
		} catch (IndexOutOfBoundsException ex) {
			return false;
		}
	}*/

	public void addLisViewId(int id) {
		Log.d("addLisViewtId", Integer.toString(id));

		_id.add(id);
	}
	
	public void setImageViewId(int id) {
		_imageViewId = id;
	}

	@Override
	public int getCount() {
		return _list.size();
	}


	@Override
	public Object getItem(int position) {
		return _list.get(position);
	}


	@Override
	public long getItemId(int position) {
		return position;
	}

	/*
	 * Internal methods
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageListView imageListView;
		
		Log.d("@log", "position : " + Integer.toString(position));

		if (convertView == null) {
			//Log.d("@log", "getView::ImageListView instance");
			imageListView = new ImageListView(_context, _listId);
			
			for (int i=0; i<_id.size(); ++i) {

				Log.d("@log", _id.get(i).toString() + " " + _list.get(position).getText(i));
				imageListView.setText(_id.get(i), _list.get(position).getText(i));
			}
			
			//Log.d("@log", "getView::ImageListView setText ok");
		} else {
			imageListView = (ImageListView) convertView;

			for (int i=0; i<_id.size(); ++i) {
				Log.d("@log", Integer.toString(i) + " " + _list.get(position).getText(i));
				imageListView.resetText(i, _list.get(position).getText(i));
			}
		}

		imageListView.setImage(_imageViewId, _list.get(position).getDrawable());
		Log.d("@log", "getView::ImageListView setImage ok");

		return imageListView;
	}


	/*
	 * Attributes
	 */
	protected ArrayList<ImageListViewType> _list;

	protected ArrayList<Integer> _id;

	protected Context _context;
	
	protected int _listId, _imageViewId;

}
